package com.xr.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xr.constant.MessageConstant;
import com.xr.exception.CustomException;
import com.xr.mapper.CheckGroupMapper;
import com.xr.mapper.CheckItemMapper;
import com.xr.pojo.CheckGroup;
import com.xr.pojo.CheckItem;
import com.xr.query.QueryPageBean;
import com.xr.result.PageResult;
import com.xr.service.CheckGroupService;
import com.xr.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
//import sun.plugin2.message.Message;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired  //本地注解注入
    private CheckGroupMapper checkGroupMapper;

//    查询所有检查组
    @Override
    public List<CheckGroup> queryAllCheckGroup() {
        return checkGroupMapper.selectByExample(null);
    }

    //    分页+条件查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(),true);
        Page<CheckGroup> list = checkGroupMapper.selectByQueryString(queryPageBean.getQueryString());
        return new PageResult(list.getTotal(), list.getResult());
    }

    //  删除检查组
    @Transactional
    @Override
    public void deleteCheckGroup(Integer id) throws CustomException {
        System.out.println("join checkGroupService==>" + id);
//        判断检查组是否被套餐使用了
        Integer count = checkGroupMapper.isUseInCheckGroupSetMeal(id);
        if (count > 0) {
//            被使用了则抛出异常 不能删除
            System.out.println("count==>" + count);
            throw new CustomException(MessageConstant.CHECKGROUP_IN_USE);
        }
//        没被使用
//        沒有引用則刪除
        checkGroupMapper.deleteByPrimaryKey(id);
    }

    //  添加
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //新增检查组
        checkGroupMapper.add(checkGroup);
        //设置检查组和检查项多对多的关联关系
        Integer checkGroupId = checkGroup.getId();
        this.setCheckGroupAndCheckItem(checkGroupId, checkitemIds);
    }

    //    根据检查组ID找检查项的ID
    @Override
    public List<Integer> selectItemsByGroupId(Integer groupId) {
        return checkGroupMapper.selectItemsByGroupId(groupId);
    }

    //    根据ID查找
    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupMapper.selectByPrimaryKey(id);
    }

    //    修改
    @Override
    public void update(CheckGroup checkGroup, Integer[] checkitemIds) {
//      1、修改检查组的基本信息
        checkGroupMapper.updateByPrimaryKeySelective(checkGroup);
//      2 、清除所有关联的检查项
        checkGroupMapper.deleteRelation(checkGroup.getId());
//      3、选中当前关联的检查项关系
        Integer checkGroupId = checkGroup.getId();
        this.setCheckGroupAndCheckItem(checkGroupId, checkitemIds);
    }

    //建立检查组和检查项的关系
    public void setCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkitemIds) {
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkGroupId", checkGroupId);
                map.put("checkitemId", checkitemId);
                checkGroupMapper.setCheckGroupAndCheckItem(map);
            }
        }
    }
}
